package com.zhijian.wharf.service.impl;

import com.zhijian.autoconfigure.cache.CacheClient;
import com.zhijian.util.JsonUtil;
import com.zhijian.wharf.entity.dto.UserDto;
import com.zhijian.wharf.enums.SystemEnum;
import com.zhijian.wharf.service.TokenService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author HCG
 * @version 1.0
 * @date 2023-05-23
 */
@Slf4j
@Service
public class TokenServiceImpl implements TokenService {

    @Autowired
    private CacheClient cacheClient;

    @Override
    public UserDto getUserDtoByToken(SystemEnum systemEnum, String token) {
        if (StringUtils.isBlank(token)) {
            return null;
        }
        String userJson = cacheClient.get(String.format(systemEnum.getUserKey(), token));
        if (StringUtils.isBlank(userJson)) {
            return null;
        }
        return JsonUtil.toBean(userJson, UserDto.class);
    }
}
